package util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * UserRank: tum0r
 * Package Name: util
 * Time: 2019-01-24 Thursday 13:56:48
 */
public class Hash {
    public static final String MD2 = "md2";
    public static final String MD5 = "md5";
    public static final String SHA1 = "sha-1";
    public static final String SHA224 = "sha-224";
    public static final String SHA256 = "sha-256";
    public static final String SHA384 = "sha-384";
    public static final String SHA512 = "sha-512";

    public String encryption(String planText, String mode) {
        StringBuilder result = new StringBuilder();
        try {
            MessageDigest messageDigest = MessageDigest.getInstance(mode);
            byte[] temp = messageDigest.digest(planText.getBytes());
            String hex;
            for (int i = 0; i < temp.length; i++) {
                hex = Integer.toHexString(temp[i] & 0xff);
                if (hex.length() == 1) {
                    result.append("0");
                }
                result.append(hex);
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return result.toString();
    }

    public String passwordEncryption(String planText) {
        return encryption(encryption(planText, Hash.SHA256), Hash.MD5);
    }
}
